package com.qf.controller;

import com.qf.captcha.CaptchaCodeManager;
import com.qf.util.Base64;
import com.qf.util.ResponseUtil;
import com.qf.util.UUID;
import com.qf.util.VerifyCodeUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @version v1.0.0
 * @Description:
 * @date: 2022.02.18
 * @since 1.0
 */
@RestController
@RequestMapping("/admin/auth/")
@CrossOrigin("http://localhost")
public class AdminAuthController {

    @RequestMapping("/captchaImage")
    public Object captchaImage() throws IOException {

        //随机验证码一位
        String verifyCode = VerifyCodeUtils.generateVerifyCode(1);
        String uuid = UUID.randomUUID().toString();
        //把验证码存储到缓存中
        CaptchaCodeManager.addToCache(uuid,verifyCode,5);

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //生成图片
        VerifyCodeUtils.outputImage(115,36,outputStream,verifyCode);

        Map<String,Object> data = new HashMap<>();
        data.put("uuid", uuid);
        data.put("img", Base64.encode(outputStream.toByteArray() ) );

        return ResponseUtil.ok(data);
    }
}
